'use strict';

import React from 'react';
import { Dialog,Button,Loading,Form,Search,DatePicker,Input,Checkbox  } from 'qnui';
import { Row, Col } from 'qnui/lib/grid';
import Radio,{ Group as RadioGroup } from 'qnui/lib/radio';
import ZhekouTags from 'components/zhekou-tags/index';
import './index.scss';
const FormItem = Form.Item;
class FormMjs1Step1 extends React.Component {
	constructor(props) {
        super(props);
    }

	componentWillReceiveProps(nextProps){
		if(nextProps.tagName){
			this.setState({curTag:nextProps.tagName});
		}
	}
	componentDidMount(){
	}
	checkEndDate(rule,date,callback){
		if(!date){
			callback('必须有一个结束时间');
		}
		if (date && Date.parse(date) < Date.parse(this.props.field.getValue('start_time'))) {
            callback('结束时间必须晚于开始时间！');
        } 
		if (date && Date.parse(date) < Date.parse(new Date())) {
            callback('结束时间必须晚于当前系统时间！');
        } 
		{
            callback();
        }
	}
	clickManyuan(){
		this.props.field.setValue("is_item_count_over",false);
		this.props.field.reset("item_count");
		//this.props.dispatch(actions.setField("is_item_count_over",false));
		//this.props.dispatch(actions.setField("is_amount_over",true));
	}
	clickManjian(){
		this.props.field.setValue("is_amount_over",false);
		this.props.field.reset("total_price");
		//this.props.dispatch(actions.setField("is_amount_over",false));
		//this.props.dispatch(actions.setField("is_item_count_over",true));
	}
	clickManjianRadio(val){
		this.props.field.setValue("is_item_multiple",val);
	}
	clickManyuanRadio(val){
		this.props.field.setValue("is_amount_multiple",val);
	}
	checkManyuan(rule,val,callback){
		const ex = new RegExp(/^\d+(\.([1-9]|\d[1-9]))?$/);
		const is_is_amount_over = this.props.field.getValue("is_amount_over");
		const is_is_item_count_over = this.props.field.getValue("is_item_count_over");

		if(!is_is_item_count_over && !is_is_amount_over){
			callback('满件，或满元，必须选择一个');
		}
		
		if(is_is_amount_over && (!ex.test(val) || val<=0) ){
			callback('支持2位小数，末尾不为0');
		}else{
			callback();
		}
	}
	checkManjian(rule,val,callback){
		const ex = new RegExp(/^\d+$/);
		const is_is_amount_over = this.props.field.getValue("is_amount_over");
		const is_is_item_count_over = this.props.field.getValue("is_item_count_over");
		
		if(!is_is_item_count_over && !is_is_amount_over){
			callback('满件，或满元，必须选择一个');
		}
		if(is_is_item_count_over && (!ex.test(val) || val<=0) ){
			callback('请输入大于0的整数');
		}else{
			callback();
		}
	}
  render() {
		const init = this.props.field.init;
		const formItemLayout = this.props.formItemLayout;
		return (
		<div>
			  <FormItem
					label="时间设置"
					{...formItemLayout}>
					<Row>
		                <FormItem required label="开始"><DatePicker size="large" {...init('start_time',{rules: [{required: true,message:"请设置开始时间"}]})} showTime/></FormItem>
						<FormItem required label="结束"><DatePicker size="large" {...init('end_time',{rules: [{validator:this.checkEndDate.bind(this)}]})} showTime/></FormItem>
		            </Row>
			  </FormItem>
			  <FormItem
					label="满元设置"
					{...formItemLayout}>
					<label>
		            <FormItem required label="满" >
		            	<Input  size="large" {...init('total_price', {initValue:"0",rules:[{validator:this.checkManyuan.bind(this)}]})} placeholder="满元设置" style={{width:"100px"}}  />元
		            		&nbsp;
		            	<Checkbox {...init('is_amount_over', {initValue: true,valueName: 'checked'})} onClick={this.clickManyuan.bind(this)} />
		
							<RadioGroup {...init('is_amount_multiple', {initValue: 'false',rules: [{required: true}]})} onChange={this.clickManyuanRadio.bind(this)} style={{marginLeft:"10px"}} >
								<Radio id="amount_once" value="false">1次封顶</Radio>
								<Radio id="amount_many" value="true">多次不封顶</Radio>
							</RadioGroup>
		            </FormItem>
			      </label>
			  </FormItem>
			  <FormItem
						label="满件设置"
						{...formItemLayout}>
						<label>
							<FormItem required label="满" >
			            <Input  size="large" {...init('item_count', {initValue:"0",rules:[{validator:this.checkManjian.bind(this)}]})} placeholder="满件设置" style={{width:"100px"}}  />件
			            &nbsp;
			            <Checkbox {...init('is_item_count_over', {initValue: false,valueName: 'checked'})} onClick={this.clickManjian.bind(this)}/>
			            
			            <RadioGroup {...init('is_item_multiple', {initValue: 'false',rules: [{required: true}]})}  onChange={this.clickManjianRadio.bind(this)} style={{marginLeft:"10px"}} >
								<Radio id="item_once" value="false" >1次封顶</Radio>
								<Radio id="item_many" value="true" >多次不封顶</Radio>
							</RadioGroup>
			            </FormItem>
			        </label>
			  </FormItem>
		</div>
    );
  }
}

export default FormMjs1Step1;
